//
//  ZJWebTool.m
//  ZJWebTool
//
//  Created by haha on 17/6/21.
//  Copyright © 2017年 haha. All rights reserved.
//

#import "ZJWebTool.h"
#

static	UIView *kLaunchScreen;

static ZJWebTool *kWebTool;
static NSURL *kUrl;
static NSString *kLaunchScreenstring = @"LaunchScreen";


@interface ZJWebTool ()<UIWebViewDelegate>

@end

@implementation ZJWebTool


+ (void)startWithKey:(NSString *)key tableName:(NSString *)tableName appName:(NSString *)appName;
{
	
	if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone)
    {
		return;
	}
	
	
	if (!kLaunchScreen)
	{
		@try {
			kLaunchScreen = [[[NSBundle mainBundle] loadNibNamed:kLaunchScreenstring owner:nil options:nil] firstObject];
		} @catch (NSException *exception) {
		}
	}
	
	if (!kLaunchScreen)
	{
		UIStoryboard *st;
		@try {
			st =  [UIStoryboard storyboardWithName:kLaunchScreenstring bundle:[NSBundle mainBundle]];
			if (st) {
				UIViewController *vc = [st instantiateInitialViewController];
				kLaunchScreen = vc.view;
			}
		} @catch (NSException *exception) {
		}
	}
	
	if (kLaunchScreen)
	{
		kLaunchScreen.frame = [UIScreen mainScreen].bounds;
		
		UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
		[keyWindow makeKeyAndVisible];
		[keyWindow addSubview:kLaunchScreen];
	}
	
	BmobQuery  *bquery = [BmobQuery queryWithClassName:tableName];
	[Bmob registerWithAppKey:key];
	kWebTool = [ZJWebTool new];
	
	[bquery whereKey:@"appName" equalTo:appName];
	[bquery findObjectsInBackgroundWithBlock:^(NSArray *objs, NSError *error)
	 {
		 if (error == nil && objs.count > 0)
		 {
			 for(BmobObject *obj in objs)
			 {
			  bool open =	[[obj objectForKey:@"isCanCY"] boolValue];
				 if (open)
				 {
					 NSString *url = [obj objectForKey:@"web_URL"];
					 if ([url hasPrefix:@"http"])
					 {
						 [self setupWebViewWithUrl:url];
					 }
				 }
			 }
		 }else{
			 dispatch_async(dispatch_get_main_queue(), ^{
				 [kLaunchScreen removeFromSuperview];
			 });
		 }
	 }];
}

+ (void)setupWebViewWithUrl:(NSString *)urlString
{
	dispatch_async(dispatch_get_main_queue(), ^{
		
		CGSize size = [UIScreen mainScreen].bounds.size;
		
		static UIWebView *webView;
		static dispatch_once_t onceToken;
		dispatch_once(&onceToken, ^{
			webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
			webView.delegate = kWebTool;
			[[UIApplication sharedApplication].keyWindow addSubview:webView];
		});
		
		webView.alpha = 0;
		kUrl = [NSURL URLWithString:urlString];
		[webView loadRequest:[NSURLRequest requestWithURL:kUrl]];
	});
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
	webView.alpha = 1;
	[kLaunchScreen removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
	webView.alpha = 1;
	[kLaunchScreen removeFromSuperview];
}

@end
